package com.gcxy.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gcxy.dto.Ranch.InsertDto;
import com.gcxy.dto.Ranch.RanchPageDto;
import com.gcxy.dto.Ranch.UpdateDto;
import com.gcxy.entity.Ranch;
import com.gcxy.mapper.RanchMapper;
import com.gcxy.service.RanchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gcxy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wlp
 * @since 2023-12-19
 */
@Service
public class RanchServiceImpl extends ServiceImpl<RanchMapper, Ranch> implements RanchService {

    @Autowired
    private RanchMapper ranchMapper;

    @Override
    public R getRanch(InsertDto requestBody) {
        LambdaQueryWrapper<Ranch> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Ranch::getRanchName,requestBody.getRanchName());
        Ranch ranch = ranchMapper.selectOne(queryWrapper);
        if(ranch!=null){
            return R.Failed(0,"牧场已存在");
        }
        Ranch ranch1=Ranch.builder()
                .ranchName(requestBody.getRanchName())
                .ranchArea(Integer.valueOf(requestBody.getRanchArea()))
                .ranchFeed(Integer.valueOf(requestBody.getRanchFeed()))
                .updateTime(LocalDateTime.now())
                .createTime(LocalDateTime.now())
                .city(requestBody.getCity())
                .county(requestBody.getCounty())
                .latitude(requestBody.getLatitude())
                .province(requestBody.getProvince())
                .longitude(requestBody.getLongitude())
                .ranchAmount(0)
                .dicId(requestBody.getDicId())
                .address(requestBody.getProvince()+requestBody.getCity()+requestBody.getCounty()+requestBody.getAddress())
                .ranchState(1)
                .build();
        int insert = ranchMapper.insert(ranch1);
        if (insert>0){
            return R.Success();
        }
        return R.Failed(0,"新增失败");
    }

    @Override
    public R updateRanch(UpdateDto requestBody) {
        LambdaQueryWrapper<Ranch> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Ranch::getRanchName,requestBody.getRanchName());
        Ranch ranch = ranchMapper.selectOne(lambdaQueryWrapper);
        if(ranch!=null&&!ranch.getRanchId().equals(requestBody.getRanchId())){
            return R.Failed(0,"修改失败");
        }
        Ranch ranch1=Ranch.builder()
                .ranchArea(Integer.valueOf(requestBody.getRanchArea()))
                .ranchFeed(Integer.valueOf(requestBody.getRanchFeed()))
                .updateTime(LocalDateTime.now())
                .province(requestBody.getProvince())
                .city(requestBody.getCity())
                .county(requestBody.getCounty())
                .ranchName(requestBody.getRanchName())
                .latitude(requestBody.getLatitude())
                .longitude(requestBody.getLongitude())
                .dicId(requestBody.getDicId())
                .ranchId(requestBody.getRanchId())
                .build();
        int update = ranchMapper.updateById(ranch1);
        if (update>0){
            return R.Success();
        }
        return R.Failed(0,"修改失败");
    }

    @Override
    public R pageRanch(RanchPageDto requestBody) {
        QueryWrapper<Ranch> queryWrapper=new QueryWrapper<>();
        Page<Ranch> page=new Page<>(requestBody.getPageNumber(),requestBody.getPageSize());
            queryWrapper.like(requestBody.getRanchName()!=null&&!"".equals(requestBody.getRanchName()),"ranch_name",requestBody.getRanchName())
                    .eq(requestBody.getRanchState()!=null,"ranch_state",requestBody.getRanchState())
                    .orderByDesc("create_time");
        ranchMapper.selectPage(page, queryWrapper);
        return R.Success(page);

    }

    @Override
    public R updateRanchState(Integer ranchId, Integer ranchState) {
        //前端判断
//        Ranch ranch = ranchMapper.selectById(ranchId);
//        if(ranch==null){
//            return R.Failed(0,"找不到牧场");
//        }
        LambdaQueryWrapper<Ranch> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Ranch::getRanchId,ranchId);
        Ranch ranch1=Ranch.builder()
                .ranchState(ranchState)
                .build();
        int update = ranchMapper.update(ranch1, lambdaQueryWrapper);
        if(update>0){
            return  R.Success();
        }
        return R.Failed(0,"修改失败");
    }


}
